const puppeteer = require("puppeteer-core");
let fs = require("fs");
let path = require("path");

async function pc() {
  const browser = await puppeteer.launch({
    headless: false, //有浏览器界面启动
    slowMo: 50, //放慢浏览器执行速度，方便测试观察
    ignoreHTTPErrors: true,
    defaultViewport: { width: 1600, height: 900 },
    width: 1600,
    args: [
      //启动 Chrome 的参数，详见上文中的介绍
      // "–no-sandbox",
      // "--start-fullscreen",
    ],
  });
  const page = await browser.newPage();
  await page.goto("http://oa.hymson.com:8012/seeyon/main.do?method=index");
  //   console.log(page, "page---------------");
  let user = await page.$("#login_username");
  let pass = await page.$("#login_password1");
  let loginbtn = await page.$("#login_button");
  console.log(loginbtn, "loginbtn");
  await user.type("18662");
  await pass.type("Bb123456");
  //$('.test').children[1]
  // await Promise.all([
  //     loginbtn.click(),
  //     page.waitForNavigation()
  // ])

  await loginbtn.click();
  await page.waitForNavigation();
  await p(1000);
  let box = await page.$("#section-header-2153426756214147826");
  console.time(1);
  await page.evaluate(async (el) => {
    function p(t = 1000) {
      return new Promise((res) => {
        setTimeout(() => {
          res();
        }, t);
      });
    }
    console.log(el, "el");

    let qb = el?.querySelector("#sectionName_-1779673595924690923");
    qb?.click();
    await p(1000);
    // setTimeout(() => {
    let more = document.body.querySelector(
      "#sectionHeaderMore2153426756214147826"
    );
    more?.children[0]?.click();

    await p(1000);

    let iframe = document.querySelectorAll("iframe")[1];
    console.log(iframe, "iframe");
    let win = iframe.contentWindow.document;
    let card = win?.querySelectorAll(".card")[7];
    card?.children[1]?.click();
    // }, 1000);
  }, box);
  console.timeEnd(1);
  let newpage = null;
  browser.once("targetcreated", (target) => (newpage = target.page()));
  await p(6 * 1000);
  console.log(newpage, "newpagenewpagenewpage");
  let body = await newpage.$("body");
  console.log(body, "body");

  // let iframe = await page.$("#15410496e738e0a66aa04abd30b99b97MoreIframe");
  // console.log(iframe, "iframe");
  // await page.evaluate(async (el) => {
  //   console.log(el, "el");
  //   let doc = el.contentWindow.document;
  //   let card = doc?.querySelectorAll(".card")[7];
  //   card?.children[1]?.click();
  // }, iframe);

  // await more?.click();

  // let tables = await page.$(".chessboardtable");
  // // console.log(tables, 'taaaa')
  // await page.evaluate((el) => {
  //   console.log(el, "el");

  //   let a = el?.querySelector("a");
  //   a?.click();
  // }, tables);
  // const newPagePromise = new Promise((res) =>
  //   browser.on("targetcreated", async (target) => {
  //     const newTarget = await target.page();
  //     // const content = await newTarget.content()
  //     res(newTarget);
  //   })
  // );
  // //点击按钮后，等待新tab对象
  // let newPage = await newPagePromise;
  // await p(2000);
}

pc();

function p(t = 1000) {
  return new Promise((res) => {
    setTimeout(() => {
      res();
    }, t);
  });
}
